﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Big2.Big2Game;
using System.Timers;

namespace Big2.Big2WfpGui
{
    /// <summary>
    /// Interaction logic for PlayerScores.xaml
    /// </summary>
    public partial class PlayerScores : UserControl
    {
        private const double DefaultSecondsToClose=0;
        public delegate void PlayerScoresClosedEvent();
        public PlayerScoresClosedEvent PlayerScoresClosed;
        private Timer timer;
        public PlayerScores()
        {
            InitializeComponent();

            //List<Player> players = new List<Player>();
            //Player p = new Player("p1");
            //p.PreviousScore = 140;
            //p.Score = 120;
            //players.Add(p);
            //players.Add(p);
            //players.Add(p);
            //players.Add(p);
            //DisplayScores(players);      
            timer = new Timer();
            
        }


        //method to dynamically display the scoreboard
        public void DisplayScores(List<Player> playerList)
        {
            DisplayScores(playerList, DefaultSecondsToClose);
        }
        public void DisplayScores(List<Player> playerList,double secondsToClose)
        {
            int position = 88;

            for (int i = 0; i < playerList.Count; i++)
            {
                //players name
                Label playerNameLabel = new Label();
                playerNameLabel.Name = "Player" + i.ToString() + "NameLabel";
                playerNameLabel.Height = 28;
                playerNameLabel.HorizontalAlignment = HorizontalAlignment.Left;
                playerNameLabel.VerticalAlignment = VerticalAlignment.Top;
                playerNameLabel.Width = 102;
                playerNameLabel.FontSize = 14;
                Canvas.SetLeft(playerNameLabel, 14);
                Canvas.SetTop(playerNameLabel, position);
                playerNameLabel.Content = playerList.ElementAt(i).Name;         //players name
                canvasContent.Children.Add(playerNameLabel);

                
                //players (previous) score
                Label playerScoreLabel = new Label();
                playerScoreLabel.Name = "Player" + i.ToString() + "CurrentLabel";
                playerScoreLabel.Height = 28;
                playerScoreLabel.HorizontalAlignment = HorizontalAlignment.Left;
                playerScoreLabel.VerticalAlignment = VerticalAlignment.Top;
                playerScoreLabel.Width = 86;
                playerScoreLabel.FontSize = 14;
                Canvas.SetLeft(playerScoreLabel, 139);
                Canvas.SetTop(playerScoreLabel, position);

                playerScoreLabel.Content = playerList.ElementAt(i).PreviousScore.ToString();
                canvasContent.Children.Add(playerScoreLabel);

                //players gains/losses score
                Label playerWonLabel = new Label();
                playerWonLabel.Name = "Player" + i.ToString() + "WonLabel";
                playerWonLabel.Height = 28;
                playerWonLabel.HorizontalAlignment = HorizontalAlignment.Left;
                playerWonLabel.VerticalAlignment = VerticalAlignment.Top;
                playerWonLabel.Width = 86;
                playerWonLabel.FontSize = 14;
                Canvas.SetLeft(playerWonLabel, 235);
                Canvas.SetTop(playerWonLabel, position);

                playerWonLabel.Content = (playerList.ElementAt(i).Score - playerList.ElementAt(i).PreviousScore).ToString();
                canvasContent.Children.Add(playerWonLabel);

                //players new current score
                Label playerTotalLabel = new Label();
                playerTotalLabel.Name = "Player" + i.ToString() + "TotalLabel";
                playerTotalLabel.Height = 28;
                playerTotalLabel.HorizontalAlignment = HorizontalAlignment.Right;
                playerTotalLabel.VerticalAlignment = VerticalAlignment.Top;
                playerTotalLabel.Width = 86;
                playerTotalLabel.FontSize = 14;
                Canvas.SetLeft(playerTotalLabel, 404);
                Canvas.SetTop(playerTotalLabel, position);

                playerTotalLabel.Content = playerList.ElementAt(i).Score.ToString();
                canvasContent.Children.Add(playerTotalLabel);

                position += 40;
            }//for

            if (secondsToClose > 0)
            {
                timer.Stop();
                timer.Interval = secondsToClose * 1000;
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                timer.Start();
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            timer.Stop();
            if (PlayerScoresClosed != null)
                PlayerScoresClosed();            
        }
        //void DisplayScores()

        
        
    }
}
